//index.js
//获取应用实例
import http from '../../../../class/Utils/Http.js'
import tips from '../../../../class/Utils/Tips.js'
import user from '../../../../class/Utils/User.js'
var WxParse = require('../../../../class/wxParse/wxParse.js');
Page({
    data: {
        mobanid: 1,
        quanmenuid: '1',
        minusStatus: 'disabled',
        num: 1,
        scrollTop: 0,
        topflag: 0,
        lang: '简体中文',
        specification: '请选择',
        array: ['简体中文', '繁体中文', '巴西', '日本'],
        offertypes: [{id: 11, name: "出厂价"}, {id: 12, name: "提货价"}, {id: 13, name: "离岸价"}],
        specifications: ['问问', 'QQ', '让人', '恩恩'],
        unit: '吨',
        units: ['吨', '升', '个'],
        flag: 0,
        terms: '',
        cid: 0,
        time: 0,
        page: 1,
        pname: '',
        guanzhu: 0,
        guanzhushu: 23569,
        captcha: 'http://toocle.y.netsun.com/index.php/home/login/cap',
        pid: 0,
        offertype: '请选择',
        offertype_id: 0,
        bt: false,
    },


    onLoad: function (p) {
        var that = this
        var cid = p.cid || 88116
        var time = p.time || 182059
        var terms = that.data.terms
        var menus = p.menus
        var mid = p.mid
        if (mid) {
          that.setData({
            mid: mid
          })
        }

        if (menus){
          that.setData({
            menus: menus
          })
        }

        if (p.quanmenuid) {
            that.setData({
                quanmenuid: p.quanmenuid
            })
        }
        if (p.pname) {
            that.setData({
                pname: p.pname
            })
        }
        that.setData({
            cid: cid,
            time: time,
            terms: terms,
            pid: p.pid
        })
        var userid = p.userid||'0'
        that.setData({
          userid: userid
        })
        http.req('/company/products', {cid: cid, time: time, terms: terms,userid:userid}, function (res) {
            console.log(res)
            WxParse.wxParse('content', 'html', res.Company.intro, that, 5);

          // 判断是否使用模板 begin
          // if (res.Company.company_style){
          //   if (res.Company.company_style.wx_style){
          //     that.setData({
          //       mobanid: res.Company.company_style.wx_style,
          //     })
          //   }
          // }else{
          //   that.setData({
          //     mobanid: 1,
          //   })
          // }
          // 判断是否使用模板 end


          //开始判断 有没有 http begin
          var newlistproducts = []
          for (var i = 0; i < res.list_products.length; i++) {
            if (res.list_products[i].pic1 && !/http/.test(res.list_products[i].pic1)) {
              res.list_products[i].pic1 = "http://img.i.album.toocle.com/230-230/" + res.list_products[i].pic1
            }
            newlistproducts.push(res.list_products[i])

          }

         //开始判断 有没有 http end




            that.setData({
                info: res,
              list: newlistproducts
            })
          console.log(that.data.mobanid)
            var user = wx.getStorageSync('user')
            if (user.log == 1) {
                that.getfollow()
            }
        });

    },


    // //事件处理函数
    // onPageScroll: function (e) {
    // var that=this;


    // if (e.scrollTop>200){

    //   that.setData({

    //     topflag: 1,

    //     })
    // } else {

    //   that.setData({

    //     topflag: 0,

    //   })

    // }
    // },
    //点击切换形式
    changearray: function (e) {
        //console.log(e)
        var that = this;
        var menuflagno = that.data.flag;
        ////console.log(menuflagno)
        that.setData({
            flag: !menuflagno,
        })

    },

    //选择语言
    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            lang: this.data.array[e.detail.value],


        })
    },

    bindPickeroffee: function (e) {
        this.setData({
            offertype: this.data.offertypes[e.detail.value].name,
            offertype_id: this.data.offertypes[e.detail.value].id,
        })
        console.log('picker发送选择改变，携带值为', this.data.offertypes[e.detail.value].id)
    },

    bindPickerspec: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({


            specification: this.data.specifications[e.detail.value],


        })
    },
    bindPickerunit: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({


            unit: this.data.units[e.detail.value],


        })
    },

    //加减
    jian: function (e) {

        var that = this
        var num = that.data.num
        if (num > 1) {
            num--;

        }
        var minusStatus = num <= 1 ? 'disabled' : 'normal'
        that.setData({

            num: num,
            minusStatus: minusStatus
        })


    },
    shuadd: function (e) {

        var that = this;
        var num = that.data.num;

        num++;
        var minusStatus = num < 1 ? 'disabled' : 'normal';

        that.setData({

            num: num,
            minusStatus: minusStatus
        })


    },
    //数量input失去焦点获取当前数
    shuinputbindblur: function (e) {
        var that = this
        var num = that.data.num;
        var dangqianshu = e.detail.value
        if (dangqianshu < 0) {
            that.setData({
                num: num
            })
        } else {
            that.setData({
                num: dangqianshu
            })
        }
    },
    //点击导航
    menuselect: function (e) {
        var that = this
        var thisid = e.currentTarget.dataset.menuid
        that.setData({
            quanmenuid: thisid
        })
    },
    changeCaptcha: function () {
        this.setData({
            captcha: 'http://toocle.y.netsun.com/index.php/home/login/cap?' + Math.random()
        })
    },
    onReachBottom: function () {
        //console.log(123)
        var that = this
        if (that.data.quanmenuid != 1) {
            return false
        }
        var page = that.data.page
        page++
        that.setData({
            page: page
        })
        wx.showLoading({
            title: '加载中',
        })

        var data = {
            terms: that.data.terms,
            cid: that.data.cid,
            time: that.data.time,
            page: page
        }
        var list = that.data.list
        console.log(data);
        http.req("/company/products", data, function (res) {
            console.log(res.list_products);
            
          //开始判断 有没有 http begin
          for (var i = 0; i < res.list_products.length; i++) {
            if (res.list_products[i].pic1 && !/http/.test(res.list_products[i].pic1)) {
              res.list_products[i].pic1 = "http://img.i.album.toocle.com/230-230/" + res.list_products[i].pic1
            }
            list.push(res.list_products[i])

          }
         //开始判断 有没有 http end

            that.setData({
                list: list
            })
            wx.hideLoading()
        })

    },
    // 获取关注状态
    getfollow: function (cd) {
        var that = this
        var data = {
            uid: wx.getStorageSync('user').userid,
            type: 1,
            fid: that.data.info.Company.id
        }
        http.reqload("/api/is_shoucang", data, function (res) {
            console.log(res)
            if (res.code == '1304') {
                that.setData({
                    guanzhu: 1,
                })
            }
        })
    },
    // 关注商品
    gofollow: function () {
        var that = this
        var data = {
            uid: wx.getStorageSync('user').userid,
            type: 1,
            fid: that.data.info.Company.id,
            fname: that.data.info.Company.name
        }
        http.reqload("/api/shoucang", data, function (res) {
            console.log(res);
            if (res.code == '200') {
                that.setData({
                    guanzhu: 1,
                })
                tips.ok(res.msg_val)
            } else {
                tips.error(res.msg_val)
            }
        })
    },
    //关注效果
    guanzhuchange: function () {
        var that = this
        user.log_user(this, function (res) {
            var guanzhu = that.data.guanzhu;
            if (guanzhu == 0) {
                that.gofollow()
            } else {
                tips.ok("你已经关注过了");
            }
        })
    },
    //去首页
    gotohome: function () {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    //去微商机
    gotoweishangji: function () {
        wx.switchTab({
            url: '/pages/weishangji/list/list'
        })
    },
    //去生意圈
    gotoshengyiquan: function () {
        wx.switchTab({
            url: '/pages/shengyiquan/list/list'
        })
    },
    //去会员中心
    gotomaincenter: function () {
        wx.switchTab({
            url: '/pages/main/menu/menu'
        })
    },
    //去名片
    gotowodemingpian: function (e) {
      var that = this
      wx.redirectTo({
        url: '/pages/mingpian/card/card?id=' + that.data.mid
      })
    },
    //去旺铺
    gotowangpu: function () {
      var that = this
      // var info = this.data.info
      // if (info.wx_user.toocle_cid == 0) {
      //   tips.error("暂无旺铺")
      //   return false
      // }
      // if(info.wx_user.toocle_cid==100387){
      //   tips.redirectTo('/pages/mingpian/wangpu/index/index?mid='+this.data.info.id);
      // }else{
      tips.redirectTo("/pages/toocle/shop/index/index?cid=" + that.data.cid + "&time=" + that.data.time + "&quanmenuid=1" + "&menus=" + that.data.menus + "&mid=" + that.data.mid );
      // }
    },
    //去生意宝服务中心
    gotofuwuzhongxin: function () {
      tips.godaohang('/pages/main/menu/menu');
    },
    //询价按钮事件
    lijigoxunjia: function (e) {
        var that = this
        var thatpname = e.currentTarget.dataset.pname
        var thatpid = e.currentTarget.dataset.pid
        that.setData({
            pname: thatpname,
            quanmenuid: 2,
            pid: thatpid
        })
    },
    formSubmit: function (e) {
        var that = this
        var clog = that.data.info.Company.login
        var pid = that.data.pid
        var type = that.data.offertype_id
        var num = that.data.num
        var unit = that.data.unit
        var formData = e.detail.value;

        user.log_user(this, function (res) {
            var user = wx.getStorageSync('user')
            //console.log(formData);
            if (!pid) {
                tips.error('请先选择商品')
            }
            var data = {
                uid: user.userid,
                clog: clog,
                name: formData.name,
                number: num,
                unit: unit,
                pid: pid,
                intro: formData.xiangxi,
                model: formData.guige,
                price_type: type
            };
            console.log(data);
            that.setData({
                bt: true
            })
            http.reqload('/api/xunjia', data, function (res) {
                console.log(res);
                that.setData({
                    bt: false
                })
                if (res.code == 200) {
                    tips.ok(res.msg_val)
                } else {
                    tips.error(res.msg_val)
                }
            });
        })
    },
    /*授权登录*/
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res

            })
            if (res.log == 0) {
                that.setData({
                    bangdingtoocletag: true
                })
            }
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.reqload("/Api/log", {
            login: zhanghao,
            passwd: mima,
            uid: wx.getStorageSync('user').userid
        }, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
    //拨打手机
    callphone: function () {
        var that = this;
        var phoneh = that.data.info.Company.tel
        console.log(phoneh)
        wx.makePhoneCall({
            phoneNumber: phoneh
        })
    },
    aa: function () {
      var that = this
        console.log('ee')
        wx.navigateTo({
          url: '/pages/toocle/shop/search/search?cid=' + that.data.cid + '&time=' + that.data.time + '&menus=' + that.data.menus + "&mid=" + that.data.mid,
        })
    },
    //获取绑定微信的手机号码
    getPhoneNumber: function (e) {
        var that = this
        user.getPhoneNumber(e, that)
    },

})
